package com.feimt.advice;


import com.feimt.common.R;
import com.feimt.exce.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalAdvice {

    /*数据库重名问题*/
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R sqlEx(SQLIntegrityConstraintViolationException e){
        log.info("异常信息：{}",e.getMessage());

        String msg = "网络连接失败，请重试";
        if(e.getMessage().contains("Duplicate entry")){
            String[] split = e.getMessage().split(" ");
            msg = "信息 "+split[2]+"已存在，请修改后在提交！";
        }

//        e.printStackTrace();
        return R.error(msg);
    }

    @ExceptionHandler(CustomException.class)
    public R customEx(CustomException e){
        log.info("异常信息：{}",e.getMessage());
        return R.error(e.getMessage());
    }

}
